package assignment.ton.gui.listener;

import java.awt.Color;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;

import javax.swing.JOptionPane;

import assignment.ton.def.Action;
import assignment.ton.def.Class;
import assignment.ton.def.Util;
import assignment.ton.gui.DrawPanel;
import assignment.ton.gui.LinkAddFrame;
import assignment.ton.gui.MainFrame;
/**
 * Listens for events in the draw panel and processes them
 * @author Tim
 */
public class DrawPanelMouseListener implements MouseListener, MouseMotionListener{
	private MainFrame mainFrame;
	private DrawPanel drawPanel;
	private final Color colorUnselected = new Color(0,205,255);
	private final Color colorSelected = new Color(0,167,255);
	private int x,y;
	public DrawPanelMouseListener(MainFrame mF, DrawPanel dP){
		mainFrame = mF;
		drawPanel = dP;
	}
	private boolean allowDragUpdate; 
	@Override
	public void mouseClicked(MouseEvent e) {
		//this section handles left click
		if(e.getModifiers()==InputEvent.BUTTON1_MASK){
			//goes through  else if chain to see what action we have 
			processLeftClick(e);
			//and here we handle right click
		}else if(e.getModifiers()==InputEvent.BUTTON3_MASK){
			//this code causes a few bugs
			if(Util.canCancel(mainFrame.getAction())){
				mainFrame.setAction(Action.NONE); //sets there to be no action
				//this bit unselects any if it's selected
				if(mainFrame.getSelected()!=null){ 
					mainFrame.getSelected().setColor(colorUnselected); 
					drawPanel.repaint();
					mainFrame.isSelected(null);
				}
			}
		}
	}
	@Override
	public void mousePressed(MouseEvent e) {
		if(e.getModifiers()==InputEvent.BUTTON1_MASK){
			//this section handles left pressed (not click)
			if(drawPanel.getClassSize()>0&&Util.canChange(mainFrame.getAction())){
				Class c = drawPanel.getClosest();
				if(c!=null&&c.cursorInBox(new Point2D.Double(e.getX(),e.getY()))){
					if(mainFrame.getSelected()!=null){ mainFrame.getSelected().setColor(colorUnselected); }
					c.setColor(colorSelected);
					mainFrame.isSelected(c);
					mainFrame.setAction(Action.CLASSSELECTED);
				}else{
					//but if it's just open area unselect
					if(mainFrame.getSelected()!=null){ 
						mainFrame.getSelected().setColor(colorUnselected); 
						mainFrame.isSelected(null);
						mainFrame.setAction(Action.NONE);
					}
				}
				drawPanel.repaint();
			}
			allowDragUpdate = true; //this allows the action to be set as dragging
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if(mainFrame.getAction()==Action.DRAGGING){
			mainFrame.setAction(mainFrame.getPreviousAction());
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		if(drawPanel.getClassSize()>0&&Util.canChange(mainFrame.getAction())){
			if(mainFrame.getSelected()!=null){
				mainFrame.getSelected().setX(e.getX());
				mainFrame.getSelected().setY(e.getY());
				drawPanel.repaint();
			}
			if(allowDragUpdate&&mainFrame.getAction()==Action.CLASSSELECTED){
				mainFrame.setAction(Action.DRAGGING);
				allowDragUpdate = false;
			}
		}
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		x=e.getX();
		y=e.getY();
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
	public int getX(){
		return x;
	}
	public int getY(){
		return y;
	}
	/**
	 * this is for processing the left click event, calling methods and setting the action
	 * @param e the mouse event being processed
	 */
	public void processLeftClick(MouseEvent e){
		//putall the left click actions here, add link etc
		if(mainFrame.getAction()==Action.ADDCLASS){
			mainFrame.setAction(Action.ADDCLASS2); //we do this so it can't popup twice
			Class c = new Class(e.getX(),e.getY(),colorUnselected,mainFrame);
			drawPanel.addClass(c);
			if(mainFrame.getSelected()!=null){ 
				mainFrame.getSelected().setColor(colorUnselected); 
				mainFrame.isSelected(null);
			}
			drawPanel.repaint();
		}else if(mainFrame.getAction()==Action.ADDLINK){
			Class c = drawPanel.getClosest();
			if(c!=null&&c.cursorInBox(new Point2D.Double(e.getX(),e.getY()))){
				mainFrame.setAction(Action.ADDLINK2);
				@SuppressWarnings("unused")
				LinkAddFrame lF = new LinkAddFrame(mainFrame, mainFrame.getSelected(), c);
			}
		}else if(mainFrame.getAction()==Action.EDITLINK){
			Class c = drawPanel.getClosest();
			if(c!=null&&c.cursorInBox(new Point2D.Double(e.getX(),e.getY()))){
				mainFrame.setAction(Action.EDITLINK2);
				@SuppressWarnings("unused")
				LinkAddFrame lF = new LinkAddFrame(mainFrame, mainFrame.getSelected(), c);
			}
		}else if(mainFrame.getAction()==Action.DELETELINK){
			Class c = drawPanel.getClosest();
			if(c!=null&&c.cursorInBox(new Point2D.Double(e.getX(),e.getY()))){
				if(!mainFrame.getSelected().deleteLink(c)){
					String error = "No link detected";
					String eT = "Error";
					JOptionPane.showMessageDialog(drawPanel, error, eT, JOptionPane.ERROR_MESSAGE);
				}
				mainFrame.setAction(mainFrame.getPreviousAction());
				drawPanel.repaint();
			}			
		}
	}
}
